import {login as loginApi} from "@/api/login.js"
import router from "@/router"
import {setTokenTime} from "@/utils/auth.js"
export default {
    namespaced:true,
    state:{
        token:localStorage.getItem('token')||'',
        expand:true,
        avatar:localStorage.getItem('avatar')||'',
    lang: localStorage.getItem('lang') || 'zh'

    },
    mutations:{
        setToken(state,token){
            state.token=token
            localStorage.setItem('token',token)
        },
        changeExpand(state){
            state.expand=!state.expand
            // console.log(state,payload,'payload')

        },
        setAvatar(state,avatar){
            state.avatar=avatar
            localStorage.setItem('avatar',avatar)
        },
        changLang (state, lang) {
            state.lang = lang
          }
    },
    actions:{
        login({commit},userInfo){
            return new Promise(()=>{
                loginApi(userInfo).then((res)=>{
                    console.log(res,'res1111')
                    commit('setToken',res?.token)
                    commit('setAvatar',res?.avatar)
                    router.replace('/')
                    setTokenTime()
                })
            })
        },
        loginOut({commit}){
            console.log('1111111')
            commit('setToken','')
            commit('setAvatar','')
            localStorage.clear()
            router.replace('/login')
        }
        
    }
}